package com.ngc.mall.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ngc.mall.bo.OrderSku;
import com.ngc.mall.common.configure.mybatisPlus.BaseDomain;
import com.ngc.mall.dto.OrderAddressDTO;
import com.ngc.mall.util.GenericAndJson;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value="`order`",autoResultMap = true)
@Builder
public class OrderDomain extends BaseDomain {
    /**
     * 微信支付订单号
     */
    private String transactionId;
    /**
     * 商品总金额
     */
    private BigDecimal totalAmount;

    /**
     * 商品总数量
     */
    private Integer totalCount;

    /**
     * 订单编号
     */
    private String orderNo;

    /**
     * 实际支付金额
     */
    private BigDecimal finalPayAmount;

    /**
     * 收货信息
     */
    @TableField(value="snap_address", typeHandler = JacksonTypeHandler.class)
    private String snapAddress;

    /**
     * sku列表
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private String snapItems;

    private String snapImg;

    private String snapTitle;


//    private Long prepayId;

    private Long userId;

    /**
     * 订单状态
     */
    private Integer status;

    /**
     * 支付时间
     */

    private LocalDateTime paymentTime;

    /**
     * 发货时间
     */
    private LocalDateTime deliveryTime;

    /**
     *  确认收货时间
     */
    private LocalDateTime receiveTime;

    /**
     * 评价时间
     */
    private LocalDateTime commentTime;

    /**
     * 订单失效时间
     */
    private Date expireTime;

    /**
     * 下单时间
     */

    private Date placedTime;

    /**
     * 订单备注
     */
    private String note;

    /**
     * 支付方式 1微信 2支付宝
     */
    private Integer payType;

    /**
     * 支付来源 1 PC端 2小程序
     */
    private Integer sourceType;

    /**
     * 物流单号
     */
    private String deliverySn;

    /**
     * 物流公司
     */
    private Long deliveryCompany;

    /**
     * 订单类型 1购买消费  2.充值升级
     */
    private Integer orderType;


    @TableLogic
    private Boolean deleted;


    public void setSnapItems(List<OrderSku> orderSkuList) {
        if (orderSkuList.isEmpty()) {
            return;
        }
        this.snapItems = GenericAndJson.objectToJson(orderSkuList);
    }

    public List<OrderSku> getSnapItems() {
        List<OrderSku> list = GenericAndJson.jsonToObject(this.snapItems,
                new TypeReference<List<OrderSku>>() {
                });
        return list;
    }


    public OrderAddressDTO getSnapAddress() {
        if (this.snapAddress == null) {
            return null;
        }
        OrderAddressDTO o = GenericAndJson.jsonToObject(this.snapAddress,
                new TypeReference<OrderAddressDTO>() {
                });
        return o;
    }

    public void setSnapAddress(OrderAddressDTO address) {
        this.snapAddress = GenericAndJson.objectToJson(address);
    }
}
